/**
 * Project Name:dsrs-businessservice-dao
 * File Name:ConsumerLogPo.java
 * Package Name:com.bwda.dsrs.businessservice.domain.po.consumerlog;
 * Date:2019年4月28日
 * Copyright (c) 2019, 江苏保旺达软件有限公司 All Rights Reserved.
 *
 */
package com.bwda.dsrs.filecenter.controller;

import com.bwda.dsrs.filecenter.annotations.NeedRole;
import com.bwda.dsrs.filecenter.api.OrgServiceApi;
import com.bwda.dsrs.filecenter.aspect.LogTypeEnum;
import com.bwda.dsrs.filecenter.aspect.Loggable;
import com.bwda.dsrs.filecenter.domain.BusinessConstants;
import com.bwda.dsrs.filecenter.domain.condition.userlog.UserLogCondition;
import com.bwda.dsrs.filecenter.domain.exception.BusinessServiceException;
import com.bwda.dsrs.filecenter.domain.form.userlog.SynLogCreateForm;
import com.bwda.dsrs.filecenter.domain.form.userlog.UserLogQueryForm;
import com.bwda.dsrs.filecenter.domain.form.userlog.UserLogTreeFrom;
import com.bwda.dsrs.filecenter.domain.po.userlog.UserLogPo;
import com.bwda.dsrs.filecenter.domain.vo.userlog.UserLogVo;
import com.bwda.dsrs.filecenter.service.BusUserLogService;
import com.bwda.dsrs.filecenter.service.LogQueryPageService;
import com.bwda.dsrs.base.centerbase.response.CentreCutPageResponse;
import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.base.util.CopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户日志
 *
 * @author liangjianyong
 * @date 2019/4/12
 */
@RestController
@RequestMapping("/b/userlog")
@Api(value = "/b/userlog", tags = "用户日志")
public class UserLogController extends BaseController {

	private final Logger logger= LoggerFactory.getLogger(UserLogController.class);

	@Resource
	private OrgServiceApi orgServiceApi;

	@Autowired
	private BusUserLogService busUserLogService;

	@Autowired
	private LogQueryPageService logQueryPageService;

	@NeedRole("auditorrole")
	@ApiOperation(value = "查询用户日志列表(带分页)",notes = "查询用户日志列表(带分页)",httpMethod = "GET")
	@Loggable(descp = "查询用户日志列表(带分页)",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryPageList", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity<CentreCutPageResponse<UserLogVo>> queryPageList(@ModelAttribute@Valid UserLogQueryForm form) throws BusinessServiceException {
		//查询数据字典看日志数据来源
		ResponseEntity sourceEntity= orgServiceApi.queryDatadictByCode(BusinessConstants.ConsumerLog.LOG_SOURCE_CODE);
		String sourceType=(String)sourceEntity.getData();

		List<UserLogVo> voList = new ArrayList<UserLogVo>();
		UserLogCondition condition = this.getConditionByQueryForm(form);
		if(BusinessConstants.ConsumerLog.ES_SOURCE.equals(sourceType)){
			//来自于es
			UserLogVo pageVo=logQueryPageService.queryList(condition);
			return getSuccessResult(getPageResponse(form, pageVo.getTotalRows(), pageVo.getRows()));
		}else{
			int count = busUserLogService.queryCount(condition);
			if (count > 0) {
				List<UserLogPo> poList = busUserLogService.queryList(condition);
				voList = CopyUtil.transfer(poList, UserLogVo.class);
			}
			return getSuccessResult(getPageResponse(form, count, voList));
		}
	}

	@NeedRole("auditorrole")
	@ApiOperation(value = "查询轨迹",notes = "查询轨迹",httpMethod = "GET")
	@Loggable(descp = "查询轨迹",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/queryGj", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity queryGj(String fileId,int type){
		List<UserLogTreeFrom> list =	busUserLogService.queryListForSy(Integer.parseInt(fileId),type);
		Map map =	busUserLogService.queryFkList(list);
		return  getSuccessResult("操作成功",map);
	}

	@NeedRole("auditorrole")
	@ApiOperation(value = "同步终端日志",notes = "同步终端日志",httpMethod = "GET")
	@Loggable(descp = "同步终端日志",type = LogTypeEnum.QUERY,include = "")
	@RequestMapping(value = "/synTerminalLog", method = {RequestMethod.GET, RequestMethod.POST})
	public ResponseEntity synTerminalLog(List<SynLogCreateForm> logCreateFormList){
//		logU

		return  getSuccessResult("操作成功");
	}


	/**
	 * ConsumerLogQueryForm转换为ConsumerLogCondition
	 *
	 * @param form
	 * @return
	 */
	private UserLogCondition getConditionByQueryForm(UserLogQueryForm form) {
		UserLogCondition condition = CopyUtil.transfer(form, UserLogCondition.class);
		return condition;
	}

}